from PySide6.QtCore import QThread, Signal
from office import excel
from typing import Literal
from pandas import read_excel


class ExcelRandomDataThread(QThread):
    """excel随机数据"""
    error = Signal(str)

    def __init__(self, parent=None):
        super().__init__(parent)
        self.columns = []
        self.rows = 0
        self.path = './fake2excel.xlsx'
        self.language = 'zh_CN'
        self.re_columns = []

    def run(self):
        try:
            excel.fake2excel(columns=self.columns, rows=self.rows, path=self.path, language=self.language)
            if self.re_columns is not None:
                pd = read_excel(self.path)
                pd.columns = self.re_columns
                pd.to_excel(self.path, index=False)
        except Exception as e:
            self.error.emit(e)

    def set_value(self, columns: list, rows: int, path: str, language: Literal['english', 'zh_CN'] = 'zh_CN',
                  re_columns: list = None):
        self.columns = columns
        self.rows = rows
        self.path = path
        self.language = language
        self.re_columns = re_columns
